home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-tk / src-tk.aos / shapes.c < prev   
C/C++ Source or Header  |  2000-02-23  |  13KB  |  517 lines

  1. #include <math.h>
  2.  
  3. #include "awindow.h"
  4.  
  5. /******************************************************************************/
  6.  
  7. void tkWireSphere(GLuint base, float radius)
  8. {
  9.   GLUquadricObj *quadObj;
  10.  
  11.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  12.   quadObj = gluNewQuadric();
  13.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  14.   gluSphere(quadObj, radius, 16, 16);
  15.   glEndList();
  16. }
  17.  
  18. /******************************************************************************/
  19.  
  20. void tkSolidSphere(GLuint base, float radius)
  21. {
  22.   GLUquadricObj *quadObj;
  23.  
  24.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  25.   quadObj = gluNewQuadric();
  26.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  27.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  28.   gluSphere(quadObj, radius, 16, 16);
  29.   glEndList();
  30. }
  31.  
  32. /******************************************************************************/
  33.  
  34. void tkWireCube(GLuint base, float size)
  35. {
  36.   static const float n[6][3] =
  37.   {
  38.     {-1.0, 0.0, 0.0},
  39.     {0.0, 1.0, 0.0},
  40.     {1.0, 0.0, 0.0},
  41.     {0.0, -1.0, 0.0},
  42.     {0.0, 0.0, 1.0},
  43.     {0.0, 0.0, -1.0}
  44.   };
  45.   static const GLint faces[6][4] =
  46.   {
  47.     {0, 1, 2, 3},
  48.     {3, 2, 6, 7},
  49.     {7, 6, 5, 4},
  50.     {4, 5, 1, 0},
  51.     {5, 6, 2, 1},
  52.     {7, 4, 0, 3}
  53.   };
  54.   float x0, x1, y0, y1, z0, z1, tmp;
  55.   float v[8][3];
  56.   int i;
  57.  
  58.   x0 = -size / 2.0;
  59.   x1 = size / 2.0;
  60.   y0 = -size / 2.0;
  61.   y1 = size / 2.0;
  62.   z0 = -size / 2.0;
  63.   z1 = size / 2.0;
  64.  
  65.   if (x0 > x1) {
  66.     tmp = x0;
  67.     x0 = x1;
  68.     x1 = tmp;
  69.   }
  70.   if (y0 > y1) {
  71.     tmp = y0;
  72.     y0 = y1;
  73.     y1 = tmp;
  74.   }
  75.   if (z0 > z1) {
  76.     tmp = z0;
  77.     z0 = z1;
  78.     z1 = tmp;
  79.   }
  80.   v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  81.   v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  82.   v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  83.   v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  84.   v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  85.   v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  86.  
  87.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  88.   for (i = 0; i < 6; i++) {
  89.     glBegin(GL_LINE_LOOP);
  90.     glNormal3fv(&n[i][0]);
  91.     glVertex3fv(&v[faces[i][0]][0]);
  92.     glNormal3fv(&n[i][0]);
  93.     glVertex3fv(&v[faces[i][1]][0]);
  94.     glNormal3fv(&n[i][0]);
  95.     glVertex3fv(&v[faces[i][2]][0]);
  96.     glNormal3fv(&n[i][0]);
  97.     glVertex3fv(&v[faces[i][3]][0]);
  98.     glEnd();
  99.   }
  100.   glEndList();
  101. }
  102.  
  103. /******************************************************************************/
  104.  
  105. void tkSolidCube(GLuint base, float size)
  106. {
  107.   static const float n[6][3] =
  108.   {
  109.     {-1.0, 0.0, 0.0},
  110.     {0.0, 1.0, 0.0},
  111.     {1.0, 0.0, 0.0},
  112.     {0.0, -1.0, 0.0},
  113.     {0.0, 0.0, 1.0},
  114.     {0.0, 0.0, -1.0}
  115.   };
  116.   static const GLint faces[6][4] =
  117.   {
  118.     {0, 1, 2, 3},
  119.     {3, 2, 6, 7},
  120.     {7, 6, 5, 4},
  121.     {4, 5, 1, 0},
  122.     {5, 6, 2, 1},
  123.     {7, 4, 0, 3}
  124.   };
  125.   float x0, x1, y0, y1, z0, z1, tmp;
  126.   float v[8][3];
  127.   int i;
  128.  
  129.   x0 = -size / 2.0;
  130.   x1 = size / 2.0;
  131.   y0 = -size / 2.0;
  132.   y1 = size / 2.0;
  133.   z0 = -size / 2.0;
  134.   z1 = size / 2.0;
  135.  
  136.   if (x0 > x1) {
  137.     tmp = x0;
  138.     x0 = x1;
  139.     x1 = tmp;
  140.   }
  141.   if (y0 > y1) {
  142.     tmp = y0;
  143.     y0 = y1;
  144.     y1 = tmp;
  145.   }
  146.   if (z0 > z1) {
  147.     tmp = z0;
  148.     z0 = z1;
  149.     z1 = tmp;
  150.   }
  151.   v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  152.   v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  153.   v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  154.   v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  155.   v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  156.   v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  157.  
  158.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  159.   for (i = 0; i < 6; i++) {
  160.     glBegin(GL_POLYGON);
  161.     glNormal3fv(&n[i][0]);
  162.     glVertex3fv(&v[faces[i][0]][0]);
  163.     glNormal3fv(&n[i][0]);
  164.     glVertex3fv(&v[faces[i][1]][0]);
  165.     glNormal3fv(&n[i][0]);
  166.     glVertex3fv(&v[faces[i][2]][0]);
  167.     glNormal3fv(&n[i][0]);
  168.     glVertex3fv(&v[faces[i][3]][0]);
  169.     glEnd();
  170.   }
  171.   glEndList();
  172. }
  173.  
  174. /******************************************************************************/
  175.  
  176. void tkWireBox(GLuint base, float width, float height, float depth)
  177. {
  178.   static const float n[6][3] =
  179.   {
  180.     {-1.0, 0.0, 0.0},
  181.     {0.0, 1.0, 0.0},
  182.     {1.0, 0.0, 0.0},
  183.     {0.0, -1.0, 0.0},
  184.     {0.0, 0.0, 1.0},
  185.     {0.0, 0.0, -1.0}
  186.   };
  187.   static const GLint faces[6][4] =
  188.   {
  189.     {0, 1, 2, 3},
  190.     {3, 2, 6, 7},
  191.     {7, 6, 5, 4},
  192.     {4, 5, 1, 0},
  193.     {5, 6, 2, 1},
  194.     {7, 4, 0, 3}
  195.   };
  196.   float x0, x1, y0, y1, z0, z1, tmp;
  197.   float v[8][3];
  198.   int i;
  199.  
  200.   x0 = -width / 2.0;
  201.   x1 = width / 2.0;
  202.   y0 = -height / 2.0;
  203.   y1 = height / 2.0;
  204.   z0 = -depth / 2.0;
  205.   z1 = depth / 2.0;
  206.  
  207.   if (x0 > x1) {
  208.     tmp = x0;
  209.     x0 = x1;
  210.     x1 = tmp;
  211.   }
  212.   if (y0 > y1) {
  213.     tmp = y0;
  214.     y0 = y1;
  215.     y1 = tmp;
  216.   }
  217.   if (z0 > z1) {
  218.     tmp = z0;
  219.     z0 = z1;
  220.     z1 = tmp;
  221.   }
  222.   v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  223.   v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  224.   v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  225.   v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  226.   v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  227.   v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  228.  
  229.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  230.   for (i = 0; i < 6; i++) {
  231.     glBegin(GL_LINE_LOOP);
  232.     glNormal3fv(&n[i][0]);
  233.     glVertex3fv(&v[faces[i][0]][0]);
  234.     glNormal3fv(&n[i][0]);
  235.     glVertex3fv(&v[faces[i][1]][0]);
  236.     glNormal3fv(&n[i][0]);
  237.     glVertex3fv(&v[faces[i][2]][0]);
  238.     glNormal3fv(&n[i][0]);
  239.     glVertex3fv(&v[faces[i][3]][0]);
  240.     glEnd();
  241.   }
  242.   glEndList();
  243. }
  244.  
  245. /******************************************************************************/
  246.  
  247. void tkSolidBox(GLuint base, float width, float height, float depth)
  248. {
  249.   static const float n[6][3] =
  250.   {
  251.     {-1.0, 0.0, 0.0},
  252.     {0.0, 1.0, 0.0},
  253.     {1.0, 0.0, 0.0},
  254.     {0.0, -1.0, 0.0},
  255.     {0.0, 0.0, 1.0},
  256.     {0.0, 0.0, -1.0}
  257.   };
  258.   static const GLint faces[6][4] =
  259.   {
  260.     {0, 1, 2, 3},
  261.     {3, 2, 6, 7},
  262.     {7, 6, 5, 4},
  263.     {4, 5, 1, 0},
  264.     {5, 6, 2, 1},
  265.     {7, 4, 0, 3}
  266.   };
  267.   float x0, x1, y0, y1, z0, z1, tmp;
  268.   float v[8][3];
  269.   int i;
  270.  
  271.   x0 = -width / 2.0;
  272.   x1 = width / 2.0;
  273.   y0 = -height / 2.0;
  274.   y1 = height / 2.0;
  275.   z0 = -depth / 2.0;
  276.   z1 = depth / 2.0;
  277.  
  278.   if (x0 > x1) {
  279.     tmp = x0;
  280.     x0 = x1;
  281.     x1 = tmp;
  282.   }
  283.   if (y0 > y1) {
  284.     tmp = y0;
  285.     y0 = y1;
  286.     y1 = tmp;
  287.   }
  288.   if (z0 > z1) {
  289.     tmp = z0;
  290.     z0 = z1;
  291.     z1 = tmp;
  292.   }
  293.   v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  294.   v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  295.   v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  296.   v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  297.   v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  298.   v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  299.  
  300.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  301.   for (i = 0; i < 6; i++) {
  302.     glBegin(GL_POLYGON);
  303.     glNormal3fv(&n[i][0]);
  304.     glVertex3fv(&v[faces[i][0]][0]);
  305.     glNormal3fv(&n[i][0]);
  306.     glVertex3fv(&v[faces[i][1]][0]);
  307.     glNormal3fv(&n[i][0]);
  308.     glVertex3fv(&v[faces[i][2]][0]);
  309.     glNormal3fv(&n[i][0]);
  310.     glVertex3fv(&v[faces[i][3]][0]);
  311.     glEnd();
  312.   }
  313.   glEndList();
  314. }
  315.  
  316. /******************************************************************************/
  317.  
  318. void tkWireTorus(GLuint base, float innerRadius, float outerRadius)
  319. {
  320.   GLint i, j;
  321.   float theta1, phi1, theta2, phi2, rings, sides;
  322.   float v0[03], v1[3], v2[3], v3[3];
  323.   float n0[3], n1[3], n2[3], n3[3];
  324.  
  325.   rings = 5;
  326.   sides = 10;
  327.  
  328.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  329.   for (i = 0; i < rings; i++) {
  330.     theta1 = (float)i *2.0 * M_PI / rings;
  331.  
  332.     theta2 = (float)(i + 1) * 2.0 * M_PI / rings;
  333.     for (j = 0; j < sides; j++) {
  334.       phi1 = (float)j *2.0 * M_PI / sides;
  335.  
  336.       phi2 = (float)(j + 1) * 2.0 * M_PI / sides;
  337.  
  338.       v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
  339.       v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
  340.       v0[2] = innerRadius * sin(phi1);
  341.  
  342.       v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
  343.       v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
  344.       v1[2] = innerRadius * sin(phi1);
  345.  
  346.       v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
  347.       v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
  348.       v2[2] = innerRadius * sin(phi2);
  349.  
  350.       v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
  351.       v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
  352.       v3[2] = innerRadius * sin(phi2);
  353.  
  354.       n0[0] = cos(theta1) * (cos(phi1));
  355.       n0[1] = -sin(theta1) * (cos(phi1));
  356.       n0[2] = sin(phi1);
  357.  
  358.       n1[0] = cos(theta2) * (cos(phi1));
  359.       n1[1] = -sin(theta2) * (cos(phi1));
  360.       n1[2] = sin(phi1);
  361.  
  362.       n2[0] = cos(theta2) * (cos(phi2));
  363.       n2[1] = -sin(theta2) * (cos(phi2));
  364.       n2[2] = sin(phi2);
  365.  
  366.       n3[0] = cos(theta1) * (cos(phi2));
  367.       n3[1] = -sin(theta1) * (cos(phi2));
  368.       n3[2] = sin(phi2);
  369.  
  370.       glBegin(GL_LINE_LOOP);
  371.       glNormal3fv(n3);
  372.       glVertex3fv(v3);
  373.       glNormal3fv(n2);
  374.       glVertex3fv(v2);
  375.       glNormal3fv(n1);
  376.       glVertex3fv(v1);
  377.       glNormal3fv(n0);
  378.       glVertex3fv(v0);
  379.       glEnd();
  380.     }
  381.   }
  382.   glEndList();
  383. }
  384.  
  385. /******************************************************************************/
  386.  
  387. void tkSolidTorus(GLuint base, float innerRadius, float outerRadius)
  388. {
  389.   GLint i, j;
  390.   float theta1, phi1, theta2, phi2, rings, sides;
  391.   float v0[03], v1[3], v2[3], v3[3];
  392.   float n0[3], n1[3], n2[3], n3[3];
  393.  
  394.   rings = 5;
  395.   sides = 10;
  396.  
  397.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  398.   for (i = 0; i < rings; i++) {
  399.     theta1 = (float)i *2.0 * M_PI / rings;
  400.  
  401.     theta2 = (float)(i + 1) * 2.0 * M_PI / rings;
  402.     for (j = 0; j < sides; j++) {
  403.       phi1 = (float)j *2.0 * M_PI / sides;
  404.  
  405.       phi2 = (float)(j + 1) * 2.0 * M_PI / sides;
  406.  
  407.       v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
  408.       v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
  409.       v0[2] = innerRadius * sin(phi1);
  410.  
  411.       v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
  412.       v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
  413.       v1[2] = innerRadius * sin(phi1);
  414.  
  415.       v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
  416.       v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
  417.       v2[2] = innerRadius * sin(phi2);
  418.  
  419.       v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
  420.       v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
  421.       v3[2] = innerRadius * sin(phi2);
  422.  
  423.       n0[0] = cos(theta1) * (cos(phi1));
  424.       n0[1] = -sin(theta1) * (cos(phi1));
  425.       n0[2] = sin(phi1);
  426.  
  427.       n1[0] = cos(theta2) * (cos(phi1));
  428.       n1[1] = -sin(theta2) * (cos(phi1));
  429.       n1[2] = sin(phi1);
  430.  
  431.       n2[0] = cos(theta2) * (cos(phi2));
  432.       n2[1] = -sin(theta2) * (cos(phi2));
  433.       n2[2] = sin(phi2);
  434.  
  435.       n3[0] = cos(theta1) * (cos(phi2));
  436.       n3[1] = -sin(theta1) * (cos(phi2));
  437.       n3[2] = sin(phi2);
  438.  
  439.       glBegin(GL_POLYGON);
  440.       glNormal3fv(n3);
  441.       glVertex3fv(v3);
  442.       glNormal3fv(n2);
  443.       glVertex3fv(v2);
  444.       glNormal3fv(n1);
  445.       glVertex3fv(v1);
  446.       glNormal3fv(n0);
  447.       glVertex3fv(v0);
  448.       glEnd();
  449.     }
  450.   }
  451.   glEndList();
  452. }
  453.  
  454. /******************************************************************************/
  455.  
  456. void tkWireCylinder(GLuint base, float radius, float height)
  457. {
  458.   GLUquadricObj *quadObj;
  459.  
  460.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  461.   glPushMatrix();
  462.   glRotatef(90.0, 1.0, 0.0, 0.0);
  463.   glTranslatef(0.0, 0.0, -1.0);
  464.   quadObj = gluNewQuadric();
  465.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  466.   gluCylinder(quadObj, radius, radius, height, 12, 2);
  467.   glPopMatrix();
  468.   glEndList();
  469. }
  470.  
  471. /******************************************************************************/
  472.  
  473. void tkSolidCylinder(GLuint base, float radius, float height)
  474. {
  475.   GLUquadricObj *quadObj;
  476.  
  477.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  478.   glPushMatrix();
  479.   glRotatef(90.0, 1.0, 0.0, 0.0);
  480.   glTranslatef(0.0, 0.0, -1.0);
  481.   quadObj = gluNewQuadric();
  482.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  483.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  484.   gluCylinder(quadObj, radius, radius, height, 12, 2);
  485.   glPopMatrix();
  486.   glEndList();
  487. }
  488.  
  489. /******************************************************************************/
  490.  
  491. void tkWireCone(GLuint base, float b, float h)
  492. {
  493.   GLUquadricObj *quadObj;
  494.  
  495.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  496.   quadObj = gluNewQuadric();
  497.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  498.   gluCylinder(quadObj, b, 0.0, h, 15, 10);
  499.   glEndList();
  500. }
  501.  
  502. /******************************************************************************/
  503.  
  504. void tkSolidCone(GLuint base, float b, float h)
  505. {
  506.   GLUquadricObj *quadObj;
  507.  
  508.   glNewList(base, GL_COMPILE_AND_EXECUTE);
  509.   quadObj = gluNewQuadric();
  510.   gluQuadricDrawStyle(quadObj, GLU_FILL);
  511.   gluQuadricNormals(quadObj, GLU_SMOOTH);
  512.   gluCylinder(quadObj, b, 0.0, h, 15, 10);
  513.   glEndList();
  514. }
  515.  
  516. /******************************************************************************/
  517.